import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import type {RouteRecordRaw} from "vue-router"
import {
  ElMessage
} from "element-plus";
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/login/index',
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/HomeView.vue'),
    children:[
      {
        path: 'index',
        name: 'login',
        component: () => import('@/views/login/index.vue'),
      }
    ]
  },
  {
    path: '/login-email',
    name: 'login-email',
    component: () => import('@/views/HomeView.vue'),
    children:[
      {
        path: 'index',
        name: 'login-email',
        component: () => import('@/views/login-email/index.vue'),
      }
    ]
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/HomeView.vue'),
    children:[
      {
        path: 'index',
        name: 'register',
        component: () => import('@/views/register/index.vue'),
      }
    ]
  },
  {
    path: '/analysis',
    name: 'analysis',
    component: () => import('@/views/IndexView.vue'),
    children:[
      {
        path: 'index',
        name: 'analysis',
        component: () => import('@/views/analysis/index.vue'),
      }
    ]
  },
]
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes,
})

router.beforeEach((to, from, next) => {
  if (to.path == '/login/index' || to.path == '/register/index' || to.path == '/login-email/index') {
    // 登录或者注册才可以往下进行
    next();
  } else {
    // 获取 token
    const token = localStorage.getItem('Authorization');
    // token 不存在
    if (token == null || token == '') {
      ElMessage.error('您还没有登录，请先登录');
      next('/login/index');
    } else {
      next();
    }
  }
});

router.afterEach((to, from) => {
  // 在导航完成后执行操作
  console.log('Navigation completed from', from, 'to', to);

  // 可以添加其他操作，例如页面分析
  // ga('send', 'pageview', to.path);
});


export default router
